<?php

class ActionMetals_Details extends BaseAction
{
    public function run()
    {
        $iUserId = Yii::app()->request->getParam('user_id');

        if ( Yii::app()->request->getIsPostRequest() )
        {
            $iLotMetalId = Yii::app()->request->getParam('lot_metal_id');
            $oLotMetal = LotMetal::model()->findByPk($iLotMetalId);
            $sDoRequest = Yii::app()->request->getParam('do');

            if ( $oLotMetal )
            {
                if ( $sDoRequest == 'close' )
                {
                    $oLotMetal->status = 'closed';
                    $oLotMetal->update();
                    Yii::app()->user->setFlash('lot_metal_closed_success', 'Объявление закрыто.');
                }

                if ( $sDoRequest == 'delete' )
                {
                    $oLotMetal->delete();
                    Yii::app()->user->setFlash('lot_metal_deleted_success', 'Объявление удалено.');
                }
            }

            Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/admin/users/metals_details', array('user_id' => $iUserId)));
        }

        $oCriteria            = new CDbCriteria();
        $oCriteria->condition = 'user_id = '.$iUserId;
        $oCriteria->order     = 'date_created DESC';

        $this->template->oDataProvider = new CActiveDataProvider('LotMetal', array
        (
            'criteria' => $oCriteria,
            'pagination' => array
            (
                'pageSize' => 100
            )
        ));

    }
}